package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.ExGoodsMapper;
import com.egao.orderB.biz.ExStorageBiz;
import com.egao.orderB.biz.StockBiz;
import com.egao.orderB.entity.ExGoods;
import com.egao.orderB.entity.ExStorage;
import com.egao.orderB.entity.Stock;
import com.egao.orderB.service.ExGoodsService;
import com.sun.org.apache.xalan.internal.lib.ExsltBase;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.ExGoodsBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 出库商品详情服务实现类
 * @author : Doman
 * @date : 2022-01-07 17:11:36
 * @version ：1.0
 */
@Service
public class ExGoodsServiceImpl /*extends ServiceImpl<ExGoodsMapper, ExGoods> */implements ExGoodsService {

	@Autowired
	private ExGoodsBiz exGoodsBiz;
	@Autowired
	private ExStorageBiz exStorageBiz;
	@Autowired
	private StockBiz stockBiz;

  @Override
  public JsonResult list(PageResult2<T> page, ExGoods exGoods, User loginUser)  throws Exception{
    return exGoodsBiz.list(page,exGoods,loginUser);
  }

  @Override
  public JsonResult list(ExGoods exGoods, User loginUser) {
    return JsonResult.ok().setData(exGoodsBiz.list(exGoods,loginUser));
  }

  @Override
  public JsonResult getById(ExGoods exGoods) {
    return JsonResult.ok().setData(exGoodsBiz.getById(exGoods));
  }

  @Transactional
  @Override
  public JsonResult save(ExGoods exGoods, User loginUser) {
    if (exGoodsBiz.save(exGoods, loginUser)) {
      return JsonResult.ok("添加成功");
    }
    return JsonResult.error("添加失败");
  }

  @Transactional
  @Override
  public JsonResult update(ExGoods exGoods, User loginUser) {
    ExGoods exGoodsOld = exGoodsBiz.getById(exGoods);
    float num = exGoodsOld.getNum() - exGoods.getNum();
    float total_weight = exGoodsOld.getTotalWeight() - exGoods.getTotalWeight();
    float total_volume = exGoodsOld.getTotalVolume() - exGoods.getTotalVolume();

    ExStorage exStorage = new ExStorage();
    exStorage.setId(exGoodsOld.getExStorageId()+"");
    exStorage.setNum(0-num);
    exStorage.setTotalGrossWeight((0-total_weight)+"");
    exStorage.setVolumeM3((0-total_volume)+"");
    exStorageBiz.updateNum(exStorage, loginUser);

    if (exGoodsBiz.update(exGoods, loginUser)) {
      Stock stock = new Stock();
      stock.setCustomerId(exGoodsOld.getCustomerId());
      stock.setSku(exGoodsOld.getSku());
      stock.setName(exGoodsOld.getName());
      stock.setPack(exGoodsOld.getPack());
      stock.setWeight(exGoodsOld.getWeight());
      stock.setLength(exGoodsOld.getLength());
      stock.setWidth(exGoodsOld.getWidth());
      stock.setHigh(exGoodsOld.getHigh());
      List<Stock> stockListUnique = stockBiz.listUnique(stock);
      stock = stockListUnique.get(0);
      stock.setNum(num);
      stockBiz.updateNum(stock, loginUser);
      return JsonResult.ok("修改成功");
    }
    return JsonResult.error("修改失败");
  }

  @Transactional
  @Override
  public JsonResult remove(ExGoods exGoods, User loginUser) {
    ExGoods exGoodsOld = exGoodsBiz.getById(exGoods);
    if (exGoodsBiz.remove(exGoods, loginUser)) {

      ExStorage exStorage = new ExStorage();
      exStorage.setId(exGoodsOld.getExStorageId()+"");
      exStorage.setNum(0-exGoodsOld.getNum());
      exStorage.setTotalGrossWeight((0-exGoodsOld.getTotalWeight())+"");
      exStorage.setVolumeM3((0-exGoodsOld.getTotalVolume())+"");
      exStorageBiz.updateNum(exStorage, loginUser);

      Stock stock = new Stock();
      stock.setCustomerId(exGoodsOld.getCustomerId());
      stock.setSku(exGoodsOld.getSku());
      stock.setName(exGoodsOld.getName());
      stock.setPack(exGoodsOld.getPack());
      stock.setWeight(exGoodsOld.getWeight());
      stock.setLength(exGoodsOld.getLength());
      stock.setWidth(exGoodsOld.getWidth());
      stock.setHigh(exGoodsOld.getHigh());
      List<Stock> stockListUnique = stockBiz.listUnique(stock);
      stock = stockListUnique.get(0);
      stock.setNum(exGoodsOld.getNum());
      stockBiz.updateNum(stock, loginUser);
      return JsonResult.ok("删除成功");
    }
    return JsonResult.error("删除失败");
  }

}
